package io.kiki.sba.registry.server.metadata.slot.assigner;

import io.kiki.sba.registry.server.metadata.slot.util.builder.SlotBuilder;
import io.kiki.sba.registry.server.metadata.slot.util.builder.SlotsBuilder;
import io.kiki.sba.registry.server.metadata.slot.util.comparator.SortType;

class FewerFollowerFirstStrategy implements ScoreStrategy<Integer> {

    final SlotsBuilder slotsBuilder;

    FewerFollowerFirstStrategy(SlotsBuilder slotsBuilder) {
        this.slotsBuilder = slotsBuilder;
    }

    @Override
    public int score(Integer slotId) {
        SlotBuilder slotBuilder = slotsBuilder.getOrCreate(slotId);
        int followerNums = slotBuilder.getFollowerDataStoreNodeIdSet().size();
        // if no followers, we leave it the least priority
        // because our strategy(assign leader) is to swap follower to leader when follower is enabled
        // if no followers, we select a new data-server to assign, that's simple and low prioritized
        /**
         * 这种情况不应该参与分配
         */
        if (followerNums == 0) {
            return Integer.MAX_VALUE;
        }
        return SortType.asc.getScore(followerNums);
    }
}
